package com.ruix.app.ui.user.address;

import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.ruix.app.R;
import com.ruix.app.adapter.superadapter.SuperAdapter;
import com.ruix.app.adapter.superadapter.internal.SuperViewHolder;
import com.ruix.app.model.RegionModel;
import com.ruix.app.ui.base.BaseActivity;

import java.io.InputStream;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

/**
 * Created by hadexs on 2017/4/18.
 */

public class ProvinceChoseActivity extends BaseActivity implements AdapterView.OnItemClickListener{


    public static Intent newIntent(Context context, String province){
        Intent intent = new Intent(context,ProvinceChoseActivity.class);
        intent.putExtra("province",province);
        return intent;
    }

    private ListView listView;
    private ProvinceListAdapter adapter;
    private String chosedProvince;
    private List<RegionModel> provinceList = null;

    @Override
    protected int loadViewLayout() {
        return R.layout.activity_country_chose;
    }

    @Override
    protected void initView() {
        listView =  (ListView)findViewById(R.id.lv_address);
        adapter = new ProvinceListAdapter(this);
        listView.setAdapter(adapter);
        setTitle("地区");
    }

    @Override
    protected void initLocalData() {
        initProvinceDatas();
        adapter.replaceAll(provinceList);
    }

    @Override
    protected void initListener() {
        listView.setOnItemClickListener(this);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        RegionModel regionModel= provinceList.get(position);
        String region = regionModel.getRegions();
        chosedProvince = regionModel.getName();
        startActivityForResult(CityChoseActivity.newIntent(this,region),10001);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 10001 && resultCode == 10012){
            if (data != null){
                String resion = data.getStringExtra("resion");
                resion = chosedProvince+resion;
                goBack(resion);
            }
        }
    }

    private void goBack(String resion){
        Intent intent = new Intent();
        intent.putExtra("resion",resion);
        setResult(10013,intent);
        finish();
    }

    class  ProvinceListAdapter extends SuperAdapter<RegionModel> {

        public ProvinceListAdapter(Context context) {
            super(context, R.layout.item_address_choose);
        }

        @Override
        public void onBind(SuperViewHolder holder, int viewType, int position, RegionModel item) {
            holder.setText(R.id.tv_address_name,item.getName());
        }
    }

    protected void initProvinceDatas() {
        AssetManager asset = this.getAssets();
        try {
            InputStream input = asset.open("region.plist");
            // 创建一个解析xml的工厂对象
            SAXParserFactory spf = SAXParserFactory.newInstance();
            // 解析xml
            SAXParser parser = spf.newSAXParser();
            RegionParserHandler handler = new RegionParserHandler();

            parser.parse(input, handler);
            input.close();
            provinceList = handler.getDataList();
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {

        }
    }
}
